<?php

namespace app\api\controller\agent;

use app\api\controller\AuthController;
use app\api\service\AgentService;
use app\api\service\PaymentService;

class AgentController extends AuthController
{
    /**
     * 申请代理
     */
    public function apply()
    {
        $param = $this->request->param();
        $user_id = $this->request->user['user_id'];
        $type = $this->request->param('type/d');
        if (empty($param['store_id'])) {
            return json(['code' => 0, 'msg' => '非法请求']);
        }

        if (empty($param['code'])) {
            return json(['code' => 0, 'msg' => '参数有误']);
        }

        if (empty($type)) {
            return json(['code' => 0, 'msg' => '请选择申请的代理类型']);
        }

        $info = AgentService::agentInfo($param['store_id'], $user_id); //查找自己是否存在代理
        if (!empty($info) && $info['status'] == 1 && $info['type'] != 4) {
            return json(['code' => 0, 'msg' => '您存在待审核的代理申请，请勿重复申请']);
        }
        if (!empty($info) && $info['status'] == 2) {
            return json(['code' => 0, 'msg' => '您已经是代理不能再申请代理']);
        }

        $info_type = AgentService::agentInfoByType($param['store_id'], $param['type'], $param['code']); //查找要申请的代理是否存在代理
        if (!empty($info_type) && $info['type'] != 4) {
            return json(['code' => 0, 'msg' => '代理已经存在，不能再申请该代理']);
        }
        $param['uid'] = $user_id;

        $rst = AgentService::applyAgent($param);
        if ($rst) {
            return json(['code' => 1, 'msg' => '申请成功待审核']);
        } else {
            return json(['code' => 0, 'msg' => '申请失败']);
        }
    }

    

    /**
     * vip代理直接付款
     */
    public function applyVipAgent()
    {
        $param = $this->request->param();
        $user_id = $this->request->user['user_id'];
        if (empty($param['store_id'])) {
            return json(['code' => 0, 'msg' => '非法请求']);
        }

        if (empty($param['code'])) {
            return json(['code' => 0, 'msg' => '参数有误']);
        }
        
        if (empty($param['payment_type'])) {
            return json(['code' => 0, 'msg' => '请选择支付方式']);
        }

        $order_no = PaymentService::generateOrderNum();
        $param['order_no'] = $order_no;
        $param['type'] = 4;
        $param['uid'] = $user_id;
        $rst = AgentService::saveTrade($param);
        if ($rst) {
            return json(['code' => 1, 'msg' => '申请成功，去支付', 'data'=>$rst]);
        } else {
            return json(['code' => 0, 'msg' => '申请失败']);
        }
    }

    /**
     *
     * 代理下的用户列表
     */
    public function userList()
    {
        $param = $this->request->param();
        $type = $this->request->param('type/d');
        $code = empty($param['code']) ? '' : $param['code'];
        if (empty($param['store_id'])) {
            return json(['code' => 0, 'msg' => '非法请求']);
        }
        if (empty($type)) {
            return json(['code' => 0, 'msg' => '请选择代理类型']);
        }

        $rst = AgentService::userList($param['store_id'], $code, $type);
        return json(['code' => 1, 'msg' => '查询成功', 'data' => $rst]);
    }

    /**
     * 根据代理类型查 名称
     */
    public function nameList()
    {
        $param = $this->request->param();
        $type = $this->request->param('type/d');
        if (empty($param['store_id'])) {
            return json(['code' => 0, 'msg' => '非法请求']);
        }
        if (empty($param['PROVINCE_CODE'])) {
            return json(['code' => 0, 'msg' => '请选择省份']);
        }
        if (empty($type)) {
            return json(['code' => 0, 'msg' => '请选择代理类型']);
        }

        $rst = AgentService::nameList($param['store_id'], $param['PROVINCE_CODE'], $type);
        return json(['code' => 1, 'msg' => '查询成功', 'data' => $rst]);
    }

    /**
     * 单独的省下的四个代理数量
     */
    public function provinceAgentList()
    {
        $param = $this->request->param();
        if (empty($param['store_id'])) {
            return json(['code' => 0, 'msg' => '非法请求']);
        }
        if (empty($param['PROVINCE_CODE'])) {
            return json(['code' => 0, 'msg' => '请选择省份']);
        }
        list($p_count, $c_count, $a_count, $v_count) = AgentService::provinceAgent($param['store_id'], $param['PROVINCE_CODE']);
        $rst = [
            ['count' => $p_count, 'type' => 1, 'PROVINCE_CODE' => $param['PROVINCE_CODE']],
            ['count' => $c_count, 'type' => 2, 'PROVINCE_CODE' => $param['PROVINCE_CODE']],
            ['count' => $a_count, 'type' => 3, 'PROVINCE_CODE' => $param['PROVINCE_CODE']],
            ['count' => $v_count, 'type' => 4, 'PROVINCE_CODE' => $param['PROVINCE_CODE']],
        ];
        return json(['code' => 1, 'msg' => '查询成功', 'data' => $rst]);
    }

    /**
     * 全国代理
     */
    public function agentList()
    {
        $param = $this->request->param();
        if (empty($param['store_id'])) {
            return json(['code' => 0, 'msg' => '非法请求']);
        }
        list($p_count, $c_count, $a_count, $v_count, $provinceArr) = AgentService::getAllList($param['store_id']);
        return json(['code' => 1, 'msg' => '查询成功', 'data' => ['p_count' => $p_count, 'c_count' => $c_count,
            'a_count' => $a_count, 'v_count' => $v_count, 'province' => $provinceArr]]);
    }
}
